
import { type PaginationProps } from "@pureadmin/table";
import { ElMessage, FormInstance } from "element-plus";
import { ref, reactive, watch, onMounted } from "vue";
import {
  CreateCollectionTaskBaseData, CollectionTaskList, CreateCollectionTask, CollectionTaskDetail, ApplicationCollectionStrategy, QueryCollectionEffect, QueryCollectionEffectStatistics
} from "@/api/stu/bankBigData";
import { useRoute } from "vue-router";
import qs from "qs";
import { log } from "console";
export function useRiskStrategic() {
  const route = useRoute();
  const dataList = ref<any>([]);
  const dataList1 = ref<any>([]);

  const loading = ref<boolean>(true);
  const CreditVisible = ref<boolean>(false);
  const DetailVisible = ref<boolean>(false);
  const regex = /^[\u4e00-\u9fa5a-zA-Z0-9]{1,50}$/;// 校验2-4个字中文
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 20,
    currentPage: 1,
    pageSizes: [10, 20, 50, 100],
    background: true
  });
  const pagination1 = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    pageSizes: [5, 10, 15, 50, 100],
    background: true
  });
  const formData = reactive({
    taskName: '',
    collectionStrategyModelId: '',
    //评级集合
  })
  const formData1 = reactive({
    collectionEfficiency: '',
    repaymentRate: '',
    //评级集合
  })
  const selectId = ref()
  const collectionStrategyModels = ref([])
  const customerTypeList = ref([])
  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      width: 120
    },
    {
      label: "催收任务名称",
      prop: "taskName",
      minWidth: 200
    },
    {
      label: "催收策略模型",
      prop: "policyName",
      minWidth: 100
    },
    {
      label: "详情",
      prop: "createTime",
      minWidth: 100,
      slot: "detail"
    },
    {
      label: "催收效果",
      fixed: "right",
      width: 100,
      slot: "operation"
    }
  ];
  const columns1: TableColumnList = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    },
    {
      label: "客户名称",
      prop: "customerName",
      minWidth: 200
    },
    {
      label: "催收难度",
      prop: "collectionDifficulty",
      minWidth: 100
    },
    {
      label: "逾期时间（天）",
      prop: "overdueTime",
      minWidth: 100
    },
    {
      label: "逾期金额（万元）",
      prop: "overdueAmount",
      minWidth: 150
    },
    {
      label: "偿还情况",
      prop: "repaymentSituation",
      minWidth: 150
    },

  ];
  const BtnLoading = ref<boolean>(false)
  function OUTBtnLoding() {
    setTimeout(() => {
      BtnLoading.value = false
    }, 1000)
  }
  // 确定新建
  async function SubmitVisible() {
    if (regex.test(formData.taskName) === false) {
      ElMessage.error('名称应输入1-50个字符')
      return
    }
    const data = {
      planId: route.query.planId,
      taskId: route.query.taskId,
      taskName: formData.taskName,
      collectionStrategyModelId: formData.collectionStrategyModelId
    }
    BtnLoading.value = true
    OUTBtnLoding()
    const res = await CreateCollectionTask(data);
    if (res.success) {
      ElMessage.success(res.msg)
      CloseVisible()
      getList()
    } else {
      ElMessage.error(res.msg)
    }
  }
  //置空表单
  function CloseVisible() {
    for (const k in formData) {
      formData[k] = ''
    }
    CreditVisible.value = false
  }

  function handleSizeChange() {
    getList()
  }
  function handleCurrentChange() {
    getList()
  }
  function handleSizeChange1() {
    getList2()
  }
  function handleCurrentChange1() {
    getList2()
  }

  async function handleCreatedModelData() {
    const data = {
      planId: route.query.planId,
      taskId: route.query.taskId,
    }
    let res = await CreateCollectionTaskBaseData(data)
    if (res.success) {
      collectionStrategyModels.value = res?.data.collectionStrategyModels
      customerTypeList.value = res?.data.customerTypeList
    } else {
      ElMessage.error(res.msg)
    }
  }


  async function getList() {
    const data = {
      pageIndex: pagination.currentPage,
      pageCount: pagination.pageSize,
      planId: route.query.planId,
      taskId: route.query.taskId,
    }
    const res = await CollectionTaskList(data);
    if (res.success) {
      res.data?.items.forEach(item => {
        item.createTime = item.createTime.substring(0, 10)
      })
      pagination.total = res.data.totalCount;
      dataList.value = res.data.items;
      loading.value = false;
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => {
        loading.value = false;
      }, 1500)
    }
  }
  async function handleApply(row) {
    const data = {
      planId: route.query.planId,
      taskId: route.query.taskId,
      detailId: row.collectionStrategyTaskId,
      studentAnswer: '学生答案'
    }
    let res = await ApplicationCollectionStrategy(data)
    if (res.status == 200) {
      ElMessage.success(res.msg);
      getList()
    } else {
      ElMessage.error(res.msg);
    }
  }
  async function ShowReportDestail(row) {
    selectId.value = row.collectionStrategyTaskId
    getList2()
    const data1 = {
      planId: route.query.planId,
      taskId: route.query.taskId,
      detailId: row.collectionStrategyTaskId,
    }
    let res1 = await QueryCollectionEffectStatistics(data1)
    if (res1.success) {
      formData1.collectionEfficiency = res1?.data.collectionEfficiency
      formData1.repaymentRate = res1?.data.repaymentRate
      DetailVisible.value = true
    } else {
      ElMessage.error(res1.msg);
    }
  }

  async function getList2() {
    const data = {
      planId: route.query.planId,
      taskId: route.query.taskId,
      detailId: selectId.value,
      pageIndex: pagination1.currentPage,
      pageCount: pagination1.pageSize,
    }
    let res = await QueryCollectionEffect(data)
    if (res.success) {
      pagination1.total = res?.data.totalCount;
      dataList1.value = res?.data.items;
    } else {
      ElMessage.error(res.msg);
    }
  }
  function CloseDetailVisible() {
    DetailVisible.value = false
  }



  onMounted(async () => {
    handleCreatedModelData()
    getList()
  })

  return {
    dataList,
    loading,
    CreditVisible,
    pagination,
    columns,
    formData,
    handleCurrentChange,
    handleSizeChange,
    SubmitVisible,
    CloseVisible,
    customerTypeList,
    collectionStrategyModels,
    DetailVisible,
    CloseDetailVisible,
    ShowReportDestail,
    handleApply,
    dataList1,
    pagination1,
    BtnLoading,
    columns1,
    formData1,
    handleCurrentChange1,
    handleSizeChange1,
  }
}
